/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ljsystem;

import databaseManager.DbConn;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 47
 */
public class UriManager {

    public UriManager() {
    }
    public String msg;

    public boolean deleteUri(String[] uids) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        int i = 0;
        if (uids.length > 0) {
            try {
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                conn.setAutoCommit(false);
                for (i = 0; i < uids.length; i++) {
                    uids[i] = new String(uids[i].getBytes("ISO-8859-1"), "UTF-8");
                    sql = "delete from uritable where uri_id=" + uids[i];
                    stmt.execute(sql);
                }
                conn.commit();
                conn.setAutoCommit(true);
                TopMenuManager.resetMenus();
                this.msg = "删除成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                try {
                    conn.rollback();
                } catch (Exception connre) {
                }
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public boolean modifyUri(String id, String uri, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = "";
        if (id != null && uri != null && memo != null) {
            try {
                id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
                uri = new String(uri.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "update uritable";
                sql += " set uri='" + uri + "',";
                sql += "memo='" + memo + "'";
                sql += " where uri_id=" + id;
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "修改成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public bo.UriItem getUriById(String id) {
        if (id == null) {
            return null;
        }
        bo.UriItem u = null;
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sql;
        try {
            id = new String(id.getBytes("ISO-8859-1"), "UTF-8");
            sql = "select u.uri_id,u.uri,ifnull(u.memo,'') as memo from uritable u where u.uri_id=" + id;
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            if (rs.next()) {
                u = new bo.UriItem();
                u.uriId = rs.getLong("uri_id");
                u.memo = rs.getString("memo");
                u.uriPath = rs.getString("uri");
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return u;
    }

    public boolean addNewUri(String uri, String memo) {
        boolean isOK = false;
        Connection conn = null;
        Statement stmt = null;
        String sql = null;
        if (uri != null && memo != null) {
            try {
                uri = new String(uri.getBytes("ISO-8859-1"), "UTF-8");
                memo = new String(memo.getBytes("ISO-8859-1"), "UTF-8");
                sql = "insert into uritable(uri,memo) values";
                sql += "('" + uri + "','" + memo + "')";
                conn = DbConn.getNewDbconn();
                stmt = conn.createStatement();
                stmt.execute(sql);
                TopMenuManager.resetMenus();
                this.msg = "添加成功";
                isOK = true;
            } catch (Exception e) {
                isOK = false;
                this.msg = e.getMessage();
            } finally {
                if (stmt != null) {
                    try {
                        stmt.close();
                    } catch (Exception stmte) {
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (Exception conne) {
                    }
                }
            }
        }
        return isOK;
    }

    public List getAllUri() {
        List list = new ArrayList();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        bo.UriItem u = null;
        String sql = "";
        try {
            sql = "select u.uri_id,u.uri,ifnull(u.memo,'') as memo from uritable u order by u.uri";
            conn = DbConn.getNewDbconn();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);//获得所有用户组信息

            while (rs.next()) {//组织列表
                u = new bo.UriItem();
                u.uriId = rs.getLong("uri_id");
                u.uriPath = rs.getString("uri");
                u.memo = rs.getString("memo");
                list.add(u);
            }
        } catch (Exception ex) {
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (Exception rse) {
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (Exception stmte) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (Exception conne) {
                }
            }
        }
        return list;
    }
}
